home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2007 June
/
PCWorld_2007-06_cd.bin
/
v cisle
/
pfrank
/
PFrankSetupSVN1.95.exe
/
{app}
/
PFrankUser.py.template
< prev
next >
Wrap
Text File
|
2007-04-09
|
12KB
|
336 lines
#!/usr/bin/python
"""
This file is used to create the user defined commands.
The procedure for creating the commands is summarized as follows:
1)
Create a class for the command using the defined API
Sample classes are provided. The samples are meticulously commented,
2)
Insert a line corresponding to your command in the UserRenamerList near the
end of this file (Don't forget the parentheses!).
3)
You then use the PFrank GUI to load the command objects from the list.
This module is written in the Python scripting language. You will therefore
have to learn some Python programming. Don't worry - it's a very easy language
to use.
An Extremely useful tutorial can be found at:
http://www.python.org/doc/current/tut/node2.html
You don't have to do this but it is highly recommended to download the python
interpreter from http://www.python.org/doc/current/tut/node2.html
The page provides a 'Download' button that leads you to an exe file.
Once installed, you can use the IDLE feature to pre-test this module to
ensure it does what you want. IDLE is a development environment that you
can use to open this module and run it. IDLE can be started by clicking on:
Start -> All Programs -> Python 2.x -> IDLE
Two sample classes are included for demonstration purposes. These can be
used as models for other functions.
"""
##########################################
# list your imported modules here.
# If you get import errors, then the modules are not
# present in the environment. In this case you will have to
# insert the required module in the PFrank install folder.
##########################################
# start of user imported modules
import re
import string
# end of user imported modules.
##########################################
# The following modules must always be imported
##########################################
# start of required imported modules
try :
from SearchRep_init import configurator
from SearchRepBase import SearchRepBase
except :
from SearchRep_initStubs import configurator
from SearchRepBaseStubs import SearchRepBase
# end of required imported modules
class UserRenamer1 (SearchRepBase) :
""" This is a sample renamer class. The SearchRepLower base class is
mandatory.
"""
def __init__ (self) :
# call base class initializer. This is mandatory
SearchRepBase.__init__(self)
# This is the ID string that will appear in the pre-defined command
# pull down list. It will also appear in the custom list when
# inserted. The id will also be used in the scan summary
# The name 'self.idstring' must not change. Set the assigned string
# on the right-hand side of the assignment statement to whatever
# name you want.
self.idstring = "(User) - Convert First 6 Chars to Upper"
# this call should remain here
self.initforscan()
def initforscan(self) :
"""
This function is called whenever a new scan or rescan is made. Use it to
reset anything that needs to be reinitialized before a scan.
This function is mandatory.
"""
pass
def fixnames(self, filename) :
"""
This function converts name of file to a new name.
Input: string representing the old filename or portion (either All, Prefix, or Extension)
The name does not include the path to the file.
Output: change indicator
True if change to input string occurred, otherwise False
string representing the new filename
When the old filename string is passed into this routine, then depending
on where this user command is placed in the custom list, the string could
be in the middle of a transformation.
e.g. if the original filename was ABCDEF.jpg, and the first command of the
custom list inserted a counter in front of the name, and the second command
was this user command, then the filename string that is passed in could be
something like 0010-ABCDEF.jpg.
This name of course no longer looks like the original name.
If you needed to extract meta data from the file, then you would not be able to
use the passed in string. Therefore the global variable 'configurator.filename' is
provided which has the full path to the filename. You can then just open that name,
extract the meta data, and then close the file.
"""
# full path to original filename
fullPathForFile = configurator.filename
#########################################################################
# Insert code to modify the filename here
#########################################################################
# here's some sample code to convert the first 6 characters of a name
# to upper case.
if len(filename) >= 7 :
first6 = filename[0:6]
remainder = filename[6:]
newname = first6.upper() + remainder
else :
# file is 6 chars long or less
newname = filename.upper()
#########################################################################
# these statements are mandatory
#########################################################################
change = False
if filename != newname :
change = True
return change, newname
# end of the class definition for Renamer1
class UserRenamer2 (SearchRepBase) :
""" This is a sample renamer class. The SearchRepLower base class is
mandatory.
"""
def __init__ (self) :
# call base class initializer. This is mandatory
SearchRepBase.__init__(self)
# This is the ID string that will appear in the pre-defined command
# pull down list. It will also appear in the custom list when
# inserted. The id will also be used in the scan summary
# The name 'self.idstring' must not change. Set the assigned string
# on the right-hand side of the assignment statement to whatever
# name you want.
self.idstring = "(User) - Add 1.1 to First Number"
self.firstNumber = re.compile(r"""
^(.*?)([0-9]+)(.*)$ # look for number in middle of string
""", re.VERBOSE)
# this call should remain here
self.initforscan()
def initforscan(self) :
"""
This function is called whenever a new scan or rescan is made. Use it to
reset anything that needs to be reinitialized before a scan.
This function is mandatory.
"""
pass
def fixnames(self, filename) :
"""
This function converts name of file to a new name.
Input: string representing the old filename or portion (either All, Prefix, or Extension)
The name does not include the path to the file.
Output: change indicator
True if change to input string occurred, otherwise False
string representing the new filename
When the old filename string is passed into this routine, then depending
on where this user command is placed in the custom list, the string could
be in the middle of a transformation.
e.g. if the original filename was ABCDEF.jpg, and the first command of the
custom list inserted a counter in front of the name, and the second command
was this user command, then the filename string that is passed in could be
something like 0010-ABCDEF.jpg.
This name of course no longer looks like the original name.
If you needed to extract meta data from the file, then you would not be able to
use the passed in string. Therefore the global variable 'configurator.filename' is
provided which has the full path to the filename. You can then just open that name,
extract the meta data, and then close the file.
"""
# full path to original filename
fullPathForFile = configurator.filename
#########################################################################
# Insert code to modify the filename here
#########################################################################
# here's some sample code to add 1.1 to the first number found in a name.
newname = filename
groups = self.firstNumber.findall(filename)
#print groups
if groups != [] :
if len(groups[0]) == 3 :
if groups[0][1] != '' :
# should be a number
number = float (groups[0][1])
number += 1.1
newnumber = str(number)
# recreate filename with calculated number
newname = groups[0][0] + newnumber + groups[0][2]
#########################################################################
# these statements are mandatory
#########################################################################
change = False
if filename != newname :
change = True
return change, newname
# end of the class definition for Renamer2
###################################################################
# Create the Renamer Objects in a list. The list is mandatory.
# The name of the list must not change.
# You can list as many names in the list as you like. PFrank will try to
# load all of them. Each name in the list must correspond to the
# name of a user command renaming class. A name can only appear
# once in the list.
#
# User Command Objects are created with the following syntax:
# ClassName()
# i.e. the name of the class followed by ()
###################################################################
UserRenamerList = [
UserRenamer1(), # object created for first user renaming class
UserRenamer2(), # object created for second user renaming class
]
######################################
# Code for testing the renamer objects
######################################
if __name__ == '__main__' :
"""
This is some test code to test the renaming objects. Test the code using
the python IDLE tool to verify that things work. Then try importing the file
to PFrank
"""
import os
userpath = os.getcwd()
userpath = os.path.normpath(userpath)
print "\
***NOTICE: Before first editing a user command file, you might need to \n\
associate an editor with .py files to prevent the unintentional execution \n\
of the user command file when trying to edit it.\n\
If the association is not made with an editor, the command window you\n\
are looking at now will shortly disappear!\n\
The user command file is called PFRankUser.py and is located at:\n\
%s\n\n\
After this association is made, you can delete this notice and the timed \n\
delay which follows.\n\
"%userpath
import time
time.sleep(12)
print "Testing User Defined Renaming Classes"
# this is a table of sample filenames for testing
# Assume that the files are in the current folder.
nameTable = [
"TestFile.txt",
"TestFile1.jpg",
"Test32File3.mp3",
"Test1.mp3",
"33T.ogg"
]
for renamer in UserRenamerList :
print "*************Testing Renamer: ", renamer.idstring
for name in nameTable :
fullpath = os.path.join(os.getcwd(), name)
configurator.filename = os.path.normpath(fullpath)
change, newname = renamer.fixnames(name)
print "Oldname is: ", name
print "Newname is: ", newname
assert (change == (not name == newname))
print ""